package com.c.ctgproject.adapter.storage

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.entity.storage.StorageMaterialInListEntity
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.setEditText
import com.c.ctgproject.utils.showToast

/**
 *作者:LiBW
 *创建日期: 2020/9/11
 *描述:仓储 物料入库adapter
 */
class StorageMaterialInAdapter(private val context: Context, private val mList: List<StorageMaterialInListEntity.DataBean.ListBean>, private val callback: Callback) : RecyclerView.Adapter<StorageMaterialInAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context)
            .inflate(R.layout.item_storage_material_in_list, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mList[position]
        holder.mTimeTV.text = data.inTime.toString()
        holder.mMaterialNameTV.text = data.materialName.toString()
        holder.mMaterialWeightTV.text = data.inWeight.toString()
        holder.mCancelBTN.setOnClickListener {
            callback.cancel(data.meterialInNo.handlerNull())
        }
        holder.mConfirmBTN.setOnClickListener {
            var number = holder.mMaterialCountTV.text.toString()
            if (number.isEmpty() || number == "" || number == "null") {
                number = "0"
            }
            callback.confirm(data.meterialInNo, data.inWeight, data.materialId, number.toInt())
            holder.mMaterialCountTV.text = setEditText("")
        }
    }

    interface Callback {
        fun confirm(materialInNo: String, inWeight: Double, materialId: Int, inNum: Int = 0)
        fun cancel(materialInNo: String)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mOrderIdTV: TextView = itemView.findViewById(R.id.mOrderIdTV)
        val mTimeTV: TextView = itemView.findViewById(R.id.mTimeTV)
        val mMaterialNameTV: TextView = itemView.findViewById(R.id.mMaterialNameTV)
        val mMaterialWeightTV: TextView = itemView.findViewById(R.id.mMaterialWeightTV)
        val mMaterialCountTV: EditText = itemView.findViewById(R.id.mMaterialCountET)
        val mCancelBTN: Button = itemView.findViewById(R.id.mCancelBTN)
        val mConfirmBTN: Button = itemView.findViewById(R.id.mConfirmBTN)
    }
}